package com.gla.service;

import com.gla.dao.CommodityMapper;
import com.gla.dao.FYH_ShopMapper;
import com.gla.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FYH_ShopService {
    private final double fw = 0.05;

    @Resource
    FYH_ShopMapper fyh_shopMapper;

    @Resource
    CommodityMapper commodityMapper;

    public List<Shop> selectAllShop(){
        return fyh_shopMapper.selectAllAhop();
    }

    public Shop selectOneShop(int shopid){
        return fyh_shopMapper.selectOneShop(shopid);
    }

    public List<Commodity> selectCommodityByShopid(int shopid, int pou){
        return fyh_shopMapper.selectCommodityByShopIdAndPOU(shopid, pou);
    }

    public Commodity selectCommodityById(int cmid){
        return commodityMapper.selectCommodityById(cmid);
    }

    public List<Commodity> selectAllCommodityByPOU(int pou){
        return fyh_shopMapper.selectAllCommodityByPOU(pou);
    }

    public List<FYH_Evaluation> selectEvaluationByCMId(int cmid){
        List<FYH_Evaluation> lfe = fyh_shopMapper.selectEvaluationByCMId(cmid);
        for(int i = 0; i < lfe.size(); i++){
            User us = fyh_shopMapper.selsetUserById(lfe.get(i).getUser_id());
            lfe.get(i).setUser_name(us.getName());
            if(us.getPhoto() == null) lfe.get(i).setUser_photo("");
            else lfe.get(i).setUser_photo(us.getPhoto());
        }
        return lfe;
    }

    public List<FYH_Vedio> selectVedioByCMId(int cmid){
        return fyh_shopMapper.selectVedioByCMId(cmid);
    }

    public FYH_ShopCar selectOneCarByCMIdAndUId(int cmid, int uid){
        return fyh_shopMapper.selectOneCarByCMIdAndUId(cmid, uid);
    }

    public FYH_ShopCar insertOneCar(int cmid, int uid){
        FYH_ShopCar fs = fyh_shopMapper.selectOneCarByCMIdAndUIdNIS0(cmid, uid);
        if(fs == null || fs.getId() == 0) fyh_shopMapper.insertOneCar(cmid, uid);
        else fyh_shopMapper.updateOneCar(fs.getId(), 1);
        return selectOneCarByCMIdAndUId(cmid, uid);
    }

    public FYH_ShopCar updateOneCar(int cid, Integer num){
        fyh_shopMapper.updateOneCar(cid, num);
        return fyh_shopMapper.selectOneCarById(cid);
    }

    public FYH_ShopCar deleteOneCar(int cid){
        fyh_shopMapper.deleteOneCar(cid);
        return fyh_shopMapper.selectOneCarById(cid);
    }

    public FYH_Vedio selectVedioById(int vid){
        return fyh_shopMapper.selectVedioById(vid);
    }

    public List<FYH_Vedio> selectVedioByType(int type, int id){
        return fyh_shopMapper.selectVedioByType(type, id);
    }

    public List<FYH_Evaluation> selectEvaluationBySPId(int spid){
        return fyh_shopMapper.selectEvaluationBySPId(spid);
    }

    public List<Shop> selectShopNear(double ula, double ulo){
        return fyh_shopMapper.selectShopNear(ula - fw, ula + fw, ulo - fw, ulo + fw);
    }
}
